﻿#include "CImage.h"

using namespace DarkStorm2D;
using namespace DarkStorm2D::GraphicsCore;

CImage::CImage()
{
	m_Type = RES_TYPE_IMAGE;
	m_Filename = "";
	
	m_Image = NULL;
	m_ColorKey = NULL;
	m_IsLoad = false;
}


CImage::~CImage()
{
	if (NULL != m_Image)
	{
		al_destroy_bitmap(m_Image);
		m_Image = NULL;
	}

	if (NULL != m_ColorKey)
	{
		delete m_ColorKey;
		m_ColorKey = NULL;
	}

	m_IsLoad = false;
}


CImage::CImage(int Width, int Height, CColor * ColorKey)
{
	Create(Width,Height,ColorKey);
}

CImage::CImage(int Width, int Height, int Flag, CColor * ColorKey)
{
	Create(Width,Height,Flag,ColorKey);
}

bool CImage::Create(int Width, int Height, CColor * ColorKey)
{
	m_IsLoad = false;
	m_Image = al_create_bitmap(Width,Height);

	if (NULL == m_Image)
		return false;

	if (NULL != ColorKey)
	{
		if (NULL != m_ColorKey)
		{
			memcpy(m_ColorKey,ColorKey,sizeof(CColor));
		}
		else 
		{
			m_ColorKey = new CColor(ColorKey);
		}
		al_convert_mask_to_alpha(m_Image, m_ColorKey->m_Color);
	}

	return true;
}

bool CImage::Create(int Width, int Height, int Flag, CColor * ColorKey)
{
	m_IsLoad = false;
	int tmpFlag = al_get_new_bitmap_flags();

	al_set_new_bitmap_flags(Flag);
	m_Image =  al_create_bitmap(Width,Height);
	al_set_new_bitmap_flags(tmpFlag);

	if (NULL != m_Image)
		return false;

	if (NULL != ColorKey)
	{
		if (NULL != m_ColorKey)
		{
			memcpy(m_ColorKey,ColorKey,sizeof(CColor));
		}
		else 
		{
			m_ColorKey = new CColor(ColorKey);
		}
		al_convert_mask_to_alpha(m_Image, m_ColorKey->m_Color);
	}

	return true;
}

const CError& CImage::LoadFromFile(const std::string & Filename)
{
	if (CSAllegroController::GetSingletonPtr()->GetInit(FLAG_ALLEGRO_IIO_ADDON))
	{
		m_Image = al_load_bitmap(Filename.c_str());
	}
	else
	{
		if (CSAllegroController::GetSingletonPtr()->Init(FLAG_ALLEGRO_IIO_ADDON))
		{
			m_Image = al_load_bitmap(Filename.c_str());
		}
	}

	if (NULL == m_Image)
	{
		CError Error(CError::TYPE_SYSTEM_ERROR,CError::FILE_LOAD);
		return Error;
	}

	m_IsLoad = true;

	CError NoError;
	return NoError;
}

const CError& CImage::LoadFromFile(const std::string & Filename, CColor* ColorKey)
{
	if (CSAllegroController::GetSingletonPtr()->GetInit(FLAG_ALLEGRO_IIO_ADDON))
	{
		m_Image = al_load_bitmap(Filename.c_str());
	}
	else
	{
		if (CSAllegroController::GetSingletonPtr()->Init(FLAG_ALLEGRO_IIO_ADDON))
		{
			m_Image = al_load_bitmap(Filename.c_str());
		}
	}

	if (NULL == m_Image)
	{
		CError Error(CError::TYPE_SYSTEM_ERROR,CError::FILE_LOAD);
		return Error;
	}

	if (NULL != ColorKey)
	{
		if (NULL != m_ColorKey)
		{
			memcpy(m_ColorKey,ColorKey,sizeof(CColor));
		}
		else 
		{
			m_ColorKey = new CColor(ColorKey);
		}
		al_convert_mask_to_alpha(m_Image, m_ColorKey->m_Color);
	}

	m_IsLoad = true;

	CError NoError;
	return NoError;
}

const CError& CImage::Reload()
{
	return LoadFromFile(m_Filename, m_ColorKey);
}

void CImage::Fill(CColor * Color)
{
	if (m_Image == (ALLEGRO_BITMAP*)al_get_target_bitmap())
	{
		al_clear_to_color(Color->m_Color);
	}
	else
	{
		ALLEGRO_STATE state;
		al_store_state(&state, ALLEGRO_STATE_TARGET_BITMAP);

		al_set_target_bitmap(m_Image);
		al_clear_to_color(Color->m_Color);

		al_restore_state(&state);
	}
}